package cn.itcast.erp.controller;

import cn.itcast.erp.annotation.CheckPower;
import cn.itcast.erp.entity.Supplier;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.exception.AuthenticationException;
import cn.itcast.erp.service.SupplierService;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * 供应商 supplier controller层
 *
 * @author 罗宇航
 * @date 2019/6/5
 */

@Controller
@RequestMapping("/supplier")
public class SupplierController {

    @Resource
    private SupplierService supplierService;

    @CheckPower("供应商")
    @ResponseBody
    @RequestMapping("/insert")
    public AjaxResult insertSupplier(@Validated Supplier supplier) {
        AjaxResult ar = new AjaxResult();

        this.supplierService.insertSupplier(supplier);
        ar.setMsg("添加成功");
        return ar;
    }


    @CheckPower("供应商")
    @ResponseBody
    @RequestMapping("/delete")
    public AjaxResult deleteSupplier(Long uuid) {
        AjaxResult ar = new AjaxResult();
        int i = this.supplierService.deleteSupplier(uuid);
        if (i > 0) {
            ar.setMsg("删除成功");
        } else {
            ar.setMsg("删除失败");
        }
        return ar;
    }

    @CheckPower("供应商")
    @ResponseBody
    @RequestMapping("/update")
    public AjaxResult updateSupplier(@Validated Supplier supplier) {
        AjaxResult ar = new AjaxResult();


        this.supplierService.updateSupplier(supplier);
        ar.setMsg("修改成功");
        return ar;
    }


    @CheckPower("供应商")
    @ResponseBody
    @RequestMapping("/find")
    public AjaxResult findSupplier(Supplier supplier) {
        // 页数 page
        // 显示行数 rows
        AjaxResult ar = this.supplierService.findSupplier(supplier);
        if (ar != null) {
            ar.setMsg("查询成功");
        } else {
            ar.setMsg("查询失败");
            ar.setTotal(0);
        }
        return ar;
    }

    @CheckPower("供应商")
    @ResponseBody
    @RequestMapping("/findAll")
    public AjaxResult findAllSuppplier(Supplier supplier){
        AjaxResult allSupplier = this.supplierService.findAllSupplier(supplier);
        return allSupplier;
    }


    //查询by id
    @CheckPower("供应商")
    @ResponseBody
    @RequestMapping("/findById")
    public Supplier findSupplierById(Long uuid) {
        return this.supplierService.findSupplierById (uuid);
    }


    //导出Excel
    @CheckPower("供应商")
    @RequestMapping("/export")
    public void getAllSupplierEx(HttpServletResponse response,int type){
        try {
            response.reset();
            response.setContentType("application/msexcel;charset=utf-8");
            if (type==1){
                response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("供应商信息.xls", "UTF-8"));
                InputStream inputStream = supplierService.getAllSupplierEx(type);
                IOUtils.copy(inputStream,response.getOutputStream());
            }else if (type ==2){
                response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("客户信息.xls", "UTF-8"));
                InputStream inputStream = supplierService.getAllSupplierEx(type);
                IOUtils.copy(inputStream,response.getOutputStream());
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
